package com.octopus.core.parser;

import com.octopus.core.Response;
import com.octopus.core.result.Result;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

/**
 * 使用DOM4J API解析XML
 *
 * @author shoulai.yang@gmail.com
 * @date 2019/07/31
 */
public class XmlParser extends TextParser {

  @Override
  public final ParseResult parse(String text, Response response) throws ParseException {
    SAXReader reader = new SAXReader();
    try {
      return this.parse(reader.read(text), response);
    } catch (DocumentException e) {
      throw new ParseException(response, e);
    }
  }

  /**
   * 使用DOM4J API解析XML
   *
   * @param document dom
   * @param response 响应信息
   * @return 解析结果
   * @throws ParseException 解析异常
   */
  public ParseResult parse(Document document, Response response) throws ParseException {
    return ParseResult.me().body(Result.me().obj(document));
  }
}
